<?php
	
	class image {
		
		function __construct() {
			
		}
		function __destruct() {
			
		}
		
		function output($path) {
			$this->output=$path;	
			return $this;
		}
		
		function input($path) {
			$this->input=$path;
			return $this;
		}
		function getProperties() {
			$this->properties=@getimagesize($this->input); // no error should it not be done...
			return $this;		
		}
		
		function create() {
			$this->getProperties();
			switch($this->properties[2]) {
				case "1": $_image = imagecreatefromgif($this->input); break;
				case "2": $_image = imagecreatefromjpeg($this->input);break;
				case "3": $_image = imagecreatefrompng($this->input); break;
				default:  $_image = imagecreatefromjpeg($this->input);	
			}
			
			$this->image	=		$_image;
			$this->width	=		imagesx($this->image);
			$this->height	=		imagesy($this->image);
			return $this;
		}
		function forceScale($scale) {
			$this->scale=$scale;
			return $this;	
		}
		function scaleWidth($width) {
			$this->scaleWidth=$width;
			return $this;
		}
		function scaleHeight($height) {
			$this->scaleHeight=$height;
			return $this;
		}
		function thumb($path) {
			$this->outpath=$path;
			$this->scale=true;
			$this->scaleHeight(150);	
			$this->scaleWidth(150);
			return $this;
		}
		function large($path) {
			$this->outpath=$path;
			$this->scale=true;
			$this->scaleHeight(600);	
			$this->scaleWidth(800);
			return $this;
		}
		function build() {
			// this build the image....	
			$this->create();
			$this->outWidth='';
			$this->outHeight='';
			
			
			if($this->scale) {
				$xar 	= $this->scaleWidth / $this->width;
				$yar 	= $this->scaleHeight / $this->height;
				$ar		= $this->width / $this->height;
				if( ($this->width <= $this->scaleWidth) && ($this->height <= $this->scaleHeight) ) {
					$_width=$this->width;
					$_height=$this->height;
				} elseif ( ($xar * $this->height) < $this->scaleHeight ) {
					 $_height = ceil($xar * $this->height);
           			 $_width = $this->scaleWidth;
				} else {
					$_width = ceil($yar * $this->width);
           			$_height = $this->scaleHeight;
				}
				$this->outWidth=$_width;
				$this->outHeight=$_height;
				
				$_tmp = imagecreatetruecolor($_width,$_height);
				if(($this->properties[2] == 1) || ($this->properties[2]==3))
				{
					imagealphablending($_tmp, false);
					imagesavealpha($_tmp,true);
					$transparent = imagecolorallocatealpha($_tmp, 255, 255, 255, 127);
					imagefilledrectangle($_tmp, 0, 0, $_width, $_height, $transparent);
				}
				imagecopyresampled($_tmp,$this->image,0,0,0,0,$_width, $_height,$this->width,$this->height);
			
				switch ($this->properties[2])
				{
					case 1: imagegif($_tmp, $this->outpath); break;
					case 2: imagejpeg($_tmp,$this->outpath, 95);  break; // best quality
					case 3: imagepng($_tmp, $this->outpath, 4); break; // no compression
					default: imagejpeg($_tmp,$this->outpath, 95); break;
				}
				imagedestroy($_tmp);
			
			} // end scaling
		}
	
	
	
	}


?>